package com.puhui.education.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;

/**
 * 弹性滑动RecyclerView
 * Created by Administrator on 2016/6/22.
 */
public class ElasticityScrollLayout extends FrameLayout {

    private float downYonInterceptTouchEvent;
    private View childView;
    private boolean isFirst=true;
    private float moveYOnTouchEvent;
    private float dyOnTouchEvent;
    private float damping = 0.6f; //阻尼系数
    private ObjectAnimator releaseObjectAnimator;
    private String scrollType;

    public ElasticityScrollLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initElasticityScrollLayout();
    }

    private void initElasticityScrollLayout() {
        ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { //检测ViewGroup是否完成初始化工作
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
                if (isFirst) {
                    if (getChildCount() != 1) {
                        throw new IllegalArgumentException("只能传入一个ScrollView或ListView或GridView或RecyclerView");
                    }
                    View child = getChildAt(0);
                    if (!(child instanceof ScrollView || child instanceof ListView
                            || child instanceof GridView || child instanceof RecyclerView)) {
                        throw new IllegalArgumentException("只能传入一个ScrollView或ListView或GridView或RecyclerView");
                    }
                    childView = child;
                    isFirst = false;
                }

            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                LogUtil.i("test", "ACTION_DOWN");
                downYonInterceptTouchEvent = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if(childView==null){
                    return false;
                }
                float moveY = ev.getY() - downYonInterceptTouchEvent;
                if(moveY<0&&!canScrollUp(childView)){
                    scrollType = "PullUp";
                    return true;

                }else if(moveY>0&&!canScrollDown(childView)){
                    scrollType = "DropDown";
                    return true;
                }

                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                moveYOnTouchEvent = event.getY();
                dyOnTouchEvent = moveYOnTouchEvent - downYonInterceptTouchEvent;
               if(scrollType.equals("PullUp")){
                   if(dyOnTouchEvent>=0){
                       childView.setTranslationY(0);
                       return true;
                   }

               }else if(scrollType.equals("DropDown")){
                   if(dyOnTouchEvent<=0){
                       childView.setTranslationY(0);
                       return true;
                   }
               }
                childView.setTranslationY(dyOnTouchEvent - dyOnTouchEvent * damping);
                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (releaseObjectAnimator == null) {
                    releaseObjectAnimator = new ObjectAnimator();
                    releaseObjectAnimator.setTarget(childView);
                    releaseObjectAnimator.setPropertyName("translationY");
                    releaseObjectAnimator.setInterpolator(new LinearInterpolator());
                }
                if(childView.getTranslationY()!=0){
                    releaseObjectAnimator.setDuration((long)Math.abs(childView.getTranslationY()*1.2));
                    releaseObjectAnimator.setFloatValues(childView.getTranslationY(),0);
                    releaseObjectAnimator.start();
                }
                scrollType = "";
                return true;
            default:
                return super.onTouchEvent(event);
        }
    }


    //是否可以上拉
    private boolean canScrollUp(View view) {
        return ViewCompat.canScrollVertically(view, 0);
    }

    //是否可以下拉
    private boolean canScrollDown(View view) {
        return ViewCompat.canScrollVertically(view, -1);
    }

}


